package com.hw.controller;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hw.pojo.User;
import com.hw.service.UserService;
import com.hw.util.R;
import com.hw.vo.UserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.netty.util.internal.StringUtil;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Date;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */
@RestController
@RequestMapping("user")
public class UserController implements Serializable {

    @Autowired
    UserService userService;


    /**
    * //TODO 登录
     * @author 国兴越
    * @date 2024/3/12 9:29
 * @param userVo
 * @return com.hw.util.R
    */

    @PostMapping("login")
    public R login(@RequestBody UserVo userVo){


        User login = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getAccount, userVo.getAccount()));


        if(login==null){
            return R.error().message("该用户不存在");
        }

        if(StringUtils.isEmpty(userVo.getAccount())){
            return R.error().message("用户为空");
        }



        if(!userVo.getPwd().equals(login.getPwd())){

            return R.ok().message("登录密码输入错误");
        }


        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("role",login.getRole()).setExpiration(new Date()).compact();



        return R.ok().message("登录成功").data("token",token).data("role",login.getRole());


    }






}
